#!/usr/bin/env python
# encoding: utf-8


from comando import ComandoResult, AbstractValidationCommand
from google.appengine.ext import db
import datetime

""" Endpoint

Description

Request

Response data

Response errors

"""
class LogToEntity(AbstractValidationCommand):

    REQUEST = "request"
    COMMAND = "command"
    
    def required(self):
        return frozenset([LogToEntity.COMMAND, LogToEntity.REQUEST])
    def optional(self):
        return frozenset([])
    def doExecute(self):
        result = ComandoResult()

        log = ComandoLog()
        log.timestamp = datetime.datetime.now()
        log.command = self.getParam(LogToEntity.COMMAND)
        requestString = unicode('', 'utf-8', 'ignore') 
        request = self.getParam(LogToEntity.REQUEST)
        for param in request:
            try:
                requestString += unicode("&", 'utf-8', 'ignore') + param + unicode("=", 'utf-8', 'ignore') + request[param]
            except TypeError:
                requestString += unicode("&", 'utf-8', 'ignore') + param + unicode("=", 'utf-8', 'ignore') + str(request[param])
                 
        
        if requestString != "":
            requestString = requestString[1:]
        
        log.request = requestString
        log.put()
        
        result.setStatus(log.is_saved())
        
        return result
    
class ComandoLog(db.Model):
    timestamp = db.DateTimeProperty()
    command = db.StringProperty()
    request = db.TextProperty()